package view;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

import controller.ConnectionTester;
import accounts.*;

public class MainFrame {

	private static JFrame frame;
	private static JPanel currPanel;
	private static JPanel oldPanel;
	
	public static void main (String[] args) {
		startup();
	}
	
	public static void startup(){
		frame = new JFrame();
		frame.setLocationRelativeTo(null);
		currPanel = new LoginPanel();
		frame.add(currPanel);
		frame.setTitle("Login");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public static void LaunchMainGUI(Authorization a){
		oldPanel = currPanel;
		if(a == Authorization.TESTER){
			frame.setTitle("Simulator");
			currPanel = new SimulationPanel();
			ConnectionTester.setSimulatorMode();
			
		} else if (a == Authorization.SECURITY_OFFICER){
			frame.setTitle("Security");
			currPanel = new SecurityPanel();
			ConnectionTester.setSecurityMode();
		} else {
			frame.setTitle("EnvironmentConfigDesigner");
			currPanel = new EnvironmentConfigDesignerPanel();
			ConnectionTester.setDesignerMode();
		}
		swapContentPanel(oldPanel, currPanel);
		frame.setJMenuBar(((MainPanel) currPanel).getMenuBar());
		frame.setSize(800, 700);
		frame.setLocationRelativeTo(null);
	}
	
	public static void LaunchSimulationGUI(){
		oldPanel = currPanel;
		frame.setTitle("Simulator");
		currPanel = new SimulationPanel();
		swapContentPanel(oldPanel, currPanel);
		frame.setJMenuBar(((MainPanel) currPanel).getMenuBar());
		frame.setSize(700, 700);
		frame.setSize(800, 700);
	}
	
	public static void LaunchEnvConfigDesignerGUI(){
		oldPanel = currPanel;
		frame.setTitle("EnvironmentConfigDesigner");
		currPanel = new EnvironmentConfigDesignerPanel();
		swapContentPanel(oldPanel, currPanel);
		frame.setJMenuBar(((MainPanel) currPanel).getMenuBar());
		frame.setSize(700, 700);
		frame.setSize(800, 700);
	}
	
	public static void LaunchSecurityGUI(){
		oldPanel = currPanel;
		frame.setTitle("Security");
		currPanel = new SecurityPanel();
		swapContentPanel(oldPanel, currPanel);
		frame.setJMenuBar(((MainPanel) currPanel).getMenuBar());
		frame.setSize(700, 700);
		frame.setSize(800, 700);
	}
	
	public static void LaunchLogin(){
		oldPanel = currPanel;
		frame.setTitle("Login");
		frame.setJMenuBar(null);
		currPanel = new LoginPanel();
		swapContentPanel(oldPanel, currPanel);
		frame.repaint();
		frame.setSize(frame.getPreferredSize());
		frame.setLocationRelativeTo(null);
	}
	
	public static void refresh(){
		int w = frame.getWidth();
		int h = frame.getHeight();
		frame.setSize(w+1,h);
		frame.setSize(w,h);
		
	}
	
	private static void swapContentPanel(JPanel oldPanel, JPanel newPanel){
		frame.getContentPane().remove(oldPanel);
		frame.add(newPanel);
		frame.getContentPane().invalidate();
		frame.getContentPane().validate();
	}
	
	public static void exit(){
		frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
	}
}
